$(function(){
    $('#ajaxForm').submit(function(){
        buscar('');
        return false;
    });
    $('#tab .remover').live('click',function(){
        remover(this);
        return false;
    });
    $('.paginateButtons a').live('click',function(){
        buscar('&'+$(this).attr('href').split('?')[1]);
        return false;
    });
    if($('#ajaxForm').is('form')){
        buscar('');
    }
});

$(function(){
   var container = $("#validateErrors");
   var validator = $('#formMenu').validate({
       errorContainer: container,
       errorLabelContainer: $("ul", container),
       wrapper: 'li',
       meta: "validate",
       rules: {
           descricao: {
               required: true
           }
       },
       messages: {
           descricao: {
               required: 'O campo DESCRIÇÃO é um campo obrigatório.'
           }
       }
    });
});

function carregando(divId){
    var elemento = $('#'+divId);
    elemento.html('<p style="margin-top: 9px;"><img src="../images/spinner.gif" /></p>');
    elemento.show();
}


function buscar(paramsPagination){
    var params = $("#ajaxForm").serialize()+paramsPagination;
    $.ajax({
        type: 'post',
        url: "dosearch",
        data: params,
        beforeSend: function(){
            carregando('updateMe');
        },
        success: function(data){
            $('#updateMe').html(data);
            $('#updateMe').show();
        }
     });
}


function remover(elem){
    if(!confirm('Deseja realmente apagar?')){
        return false;
    }
    $.ajax({
        type: 'post',
        url: "delete",
        data: 'id='+$(elem).attr('id'),
        beforeSend: function(){
            carregando('updateMe');
        },
        success: function(data){
            buscar('');
        }
     });
}